﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace OA.Utility
{
    public class Security
    {
        /// MD5 加密静态方法
        /// </summary>
        /// <param name="encryptString">待加密的密文</param>
        /// <returns>returns</returns>
        public static string MD5Encrypt(string encryptString)
        {
            if (string.IsNullOrEmpty(encryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            MD5 mClassMD5 = new MD5CryptoServiceProvider();

            string mStrEncrypt = "";

            try
            {
                mStrEncrypt =
                    BitConverter.ToString(mClassMD5.ComputeHash(Encoding.Default.GetBytes(encryptString))).Replace(
                        "-", "");
            }
            catch (ArgumentException ex)
            {
                //MoziLog.GetLogger("MoziPrintServer").LogError(ex.Message);
            }
            catch (CryptographicException ex)
            {
                //MoziLog.GetLogger("MoziPrintServer").LogError(ex.Message);
            }
            catch (Exception ex)
            {
                //MoziLog.GetLogger("MoziPrintServer").LogError(ex.Message);
            }
            finally
            {
                mClassMD5.Clear();
            }

            return mStrEncrypt;
        }

        /// <summary>
        /// DES 加密(数据加密标准，速度较快，适用于加密大量数据的场合)
        /// </summary>
        /// <param name="encryptString">待加密的密文</param>
        /// <param name="encryptKey">加密的密钥</param>
        /// <returns>returns</returns>
        public static string DESEncrypt(string encryptString, string encryptKey)
        {
            if (string.IsNullOrEmpty(encryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(encryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            if (encryptKey.Length != 8)
            {
                throw (new Exception("密钥必须为8位"));
            }

            byte[] mBtIv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            string mStrEncrypt = "";

            DESCryptoServiceProvider mDESProvider = new DESCryptoServiceProvider();

            try
            {
                byte[] mBtEncryptString = Encoding.Default.GetBytes(encryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCstream = new CryptoStream(mStream,
                                                          mDESProvider.CreateEncryptor(
                                                              Encoding.Default.GetBytes(encryptKey), mBtIv),
                                                          CryptoStreamMode.Write);

                mCstream.Write(mBtEncryptString, 0, mBtEncryptString.Length);

                mCstream.FlushFinalBlock();

                mStrEncrypt = Convert.ToBase64String(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCstream.Close();
                mCstream.Dispose();
            }
            catch (IOException ex)
            {
              
            }
            catch (CryptographicException ex)
            {
               
            }
            catch (ArgumentException ex)
            {
              
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                mDESProvider.Clear();
            }

            return mStrEncrypt;
        }

        /// <summary>
        /// DES 解密(数据加密标准，速度较快，适用于加密大量数据的场合)
        /// </summary>
        /// <param name="decryptString">待解密的密文</param>
        /// <param name="decryptKey">解密的密钥</param>
        /// <returns>returns</returns>
        public static string DESDecrypt(string decryptString, string decryptKey)
        {
            if (string.IsNullOrEmpty(decryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(decryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            if (decryptKey.Length != 8)
            {
                throw (new Exception("密钥必须为8位"));
            }

            byte[] mBtIv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            string mStrDecrypt = "";

            DESCryptoServiceProvider mDESProvider = new DESCryptoServiceProvider();

            try
            {
                byte[] mBtDecryptString = Convert.FromBase64String(decryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCstream = new CryptoStream(mStream,
                                                          mDESProvider.CreateDecryptor(
                                                              Encoding.Default.GetBytes(decryptKey), mBtIv),
                                                          CryptoStreamMode.Write);

                mCstream.Write(mBtDecryptString, 0, mBtDecryptString.Length);

                mCstream.FlushFinalBlock();

                mStrDecrypt = Encoding.Default.GetString(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCstream.Close();
                mCstream.Dispose();
            }
            catch (IOException ex)
            {
              
            }
            catch (CryptographicException ex)
            {
              
            }
            catch (ArgumentException ex)
            {
           
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                mDESProvider.Clear();
            }

            return mStrDecrypt;
        }

        /// <summary>
        /// RC2 加密(用变长密钥对大量数据进行加密)
        /// </summary>
        /// <param name="encryptString">待加密密文</param>
        /// <param name="encryptKey">加密密钥</param>
        /// <returns>returns</returns>
        public static string RC2Encrypt(string encryptString, string encryptKey)
        {
            if (string.IsNullOrEmpty(encryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(encryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            if (encryptKey.Length < 5 || encryptKey.Length > 16)
            {
                throw (new Exception("密钥必须为5-16位"));
            }

            string mStrEncrypt = "";

            byte[] mBtIv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            RC2CryptoServiceProvider mRC2Provider = new RC2CryptoServiceProvider();

            try
            {
                byte[] mBtEncryptString = Encoding.Default.GetBytes(encryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCstream = new CryptoStream(mStream,
                                                          mRC2Provider.CreateEncryptor(
                                                              Encoding.Default.GetBytes(encryptKey), mBtIv),
                                                          CryptoStreamMode.Write);

                mCstream.Write(mBtEncryptString, 0, mBtEncryptString.Length);

                mCstream.FlushFinalBlock();

                mStrEncrypt = Convert.ToBase64String(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCstream.Close();
                mCstream.Dispose();
            }
            catch (IOException ex)
            {
                
            }
            catch (CryptographicException ex)
            {
      
            }
            catch (ArgumentException ex)
            {
               
            }
            catch (Exception ex)
            {
    
            }
            finally
            {
                mRC2Provider.Clear();
            }

            return mStrEncrypt;
        }

        /// <summary>
        /// RC2 解密(用变长密钥对大量数据进行加密)
        /// </summary>
        /// <param name="decryptString">待解密密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <returns>returns</returns>
        public static string RC2Decrypt(string decryptString, string decryptKey)
        {
            if (string.IsNullOrEmpty(decryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(decryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            if (decryptKey.Length < 5 || decryptKey.Length > 16)
            {
                throw (new Exception("密钥必须为5-16位"));
            }

            byte[] mBtIv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            string mStrDecrypt = "";

            RC2CryptoServiceProvider mRC2Provider = new RC2CryptoServiceProvider();

            try
            {
                byte[] mBtDecryptString = Convert.FromBase64String(decryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCstream = new CryptoStream(mStream,
                                                          mRC2Provider.CreateDecryptor(
                                                              Encoding.Default.GetBytes(decryptKey), mBtIv),
                                                          CryptoStreamMode.Write);

                mCstream.Write(mBtDecryptString, 0, mBtDecryptString.Length);

                mCstream.FlushFinalBlock();

                mStrDecrypt = Encoding.Default.GetString(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCstream.Close();
                mCstream.Dispose();
            }
            catch (IOException ex)
            {
               
            }
            catch (CryptographicException ex)
            {
            
            }
            catch (ArgumentException ex)
            {
              
            }
            catch (Exception ex)
            {
          
            }
            finally
            {
                mRC2Provider.Clear();
            }
            return mStrDecrypt;
        }

        /// <summary>
        /// 3DES 加密(基于DES，对一块数据用三个不同的密钥进行三次加密，强度更高)
        /// </summary>
        /// <param name="encryptString">待加密密文</param>
        /// <param name="encryptKey1">密钥一</param>
        /// <param name="encryptKey2">密钥二</param>
        /// <param name="encryptKey3">密钥三</param>
        /// <returns>returns</returns>
        public static string DES3Encrypt(string encryptString, string encryptKey1, string encryptKey2,
                                         string encryptKey3)
        {
            string mStrEncrypt = "";

            try
            {
                mStrEncrypt = DESEncrypt(encryptString, encryptKey3);

                mStrEncrypt = DESEncrypt(mStrEncrypt, encryptKey2);

                mStrEncrypt = DESEncrypt(mStrEncrypt, encryptKey1);
            }
            catch (Exception ex)
            {

            }

            return mStrEncrypt;
        }

        /// <summary>
        /// 3DES 解密(基于DES，对一块数据用三个不同的密钥进行三次加密，强度更高)
        /// </summary>
        /// <param name="decryptString">待解密密文</param>
        /// <param name="decryptKey1">密钥一</param>
        /// <param name="decryptKey2">密钥二</param>
        /// <param name="decryptKey3">密钥三</param>
        /// <returns>returns</returns>
        public static string DES3Decrypt(string decryptString, string decryptKey1, string decryptKey2,
                                         string decryptKey3)
        {
            string mStrDecrypt = "";

            try
            {
                mStrDecrypt = DESDecrypt(decryptString, decryptKey1);

                mStrDecrypt = DESDecrypt(mStrDecrypt, decryptKey2);

                mStrDecrypt = DESDecrypt(mStrDecrypt, decryptKey3);
            }
            catch (Exception ex)
            {
             
            }

            return mStrDecrypt;
        }

        /// <summary>
        /// AES 加密(高级加密标准，是下一代的加密算法标准，速度快，安全级别高，目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="encryptString">待加密密文</param>
        /// <param name="encryptKey">加密密钥</param>
        /// <returns></returns>
        public static string AESEncrypt(string encryptString, string encryptKey)
        {
            if (string.IsNullOrEmpty(encryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(encryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            string mStrEncrypt = "";

            byte[] mBtIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");

            Rijndael mAESProvider = Rijndael.Create();

            try
            {
                byte[] mBtEncryptString = Encoding.Default.GetBytes(encryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCsstream = new CryptoStream(mStream,
                                                           mAESProvider.CreateEncryptor(
                                                               Encoding.Default.GetBytes(encryptKey), mBtIv),
                                                           CryptoStreamMode.Write);

                mCsstream.Write(mBtEncryptString, 0, mBtEncryptString.Length);
                mCsstream.FlushFinalBlock();

                mStrEncrypt = Convert.ToBase64String(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCsstream.Close();
                mCsstream.Dispose();
            }
            catch (IOException ex)
            {
                
            }
            catch (CryptographicException ex)
            {
               
            }
            catch (ArgumentException ex)
            {
               
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
                mAESProvider.Clear();
            }

            return mStrEncrypt;
        }

        /// <summary>
        /// AES 解密(高级加密标准，是下一代的加密算法标准，速度快，安全级别高，目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="decryptString">待解密密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <returns></returns>
        public static string AESDecrypt(string decryptString, string decryptKey)
        {
            if (string.IsNullOrEmpty(decryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(decryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            string mStrDecrypt = "";

            byte[] mBtIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");

            Rijndael mAESProvider = Rijndael.Create();

            try
            {
                byte[] mBtDecryptString = Convert.FromBase64String(decryptString);

                MemoryStream mStream = new MemoryStream();

                CryptoStream mCsstream = new CryptoStream(mStream,
                                                           mAESProvider.CreateDecryptor(
                                                               Encoding.Default.GetBytes(decryptKey), mBtIv),
                                                           CryptoStreamMode.Write);

                mCsstream.Write(mBtDecryptString, 0, mBtDecryptString.Length);
                mCsstream.FlushFinalBlock();

                mStrDecrypt = Encoding.Default.GetString(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCsstream.Close();
                mCsstream.Dispose();
            }
            catch (IOException ex)
            {
             
            }
            catch (CryptographicException ex)
            {
            
            }
            catch (ArgumentException ex)
            {
                
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                mAESProvider.Clear();
            }

            return mStrDecrypt;
        }

        internal static string DESEncrypt(string p)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
